class PagesController < ApplicationController
  before_filter :login_required
  before_filter :admin_required, :only => [:new, :create, :destroy]
  
	def show
		@page = Page.find(params[:id])
		
		if logged_in?
			@section_pages = @page.section.section_pages
		else
			@section_pages = Array.new
			@page.section.section_pages.each do |page|
				if page.published?
					@section_pages << page
				end
			end
			
			unless @page.published?
				render :file => "#{RAILS_ROOT}/public/404.html", :status => '404 Not Found'
			end
		end
	end
	
	def edit
		@page = Page.find(params[:id])
	end
	
	def new
	  @page =  Page.new
	end
	
	def create
		@page = Page.create(params[:page])
		   
		@timeout = false
		# Check to see if a Page is a News page.
		# If true, then automatically set a News page to be published.
		if @page.news?
   	  @page.set_published
   	else
   	  @page.set_draft
 	  end
 	  
   	@page.home = false
   	params[:page][:datasheet_ids] ||= []
  
  	if @page.save
	    
	    unless @page.news?
	      @url_name = url_for(:controller => :home, :action => :index).to_s       
	      
	      begin
          @page.send_email("edit", @url_name)  
        rescue Timeout::Error
          flash[:notice] = "Email Server is not responding.  Please send email manually." 
          @timeout = true
        end  
        
      end
	    
	    if @page.news?
	      flash[:notice] = "News was successfully created."
	      redirect_to news_path(@page)
      else 
        if @timeout == false
          flash[:notice] = "Page was successfully created."
        end
	      redirect_to page_path(@page)
      end
  
    else
      render :action => "new"
    end
    
  end

	def update 
	  
	  @timeout = false
    params[:page][:datasheet_ids] ||= []
		
    @page = Page.find(params[:id])
    
    unless @page.news?
      if @page.home == false
  		  @page.state = "draft"
  	  end
    end

		if params[:review]
			@page.state = "pending"
		end
    
    if @page.update_attributes(params[:page])
      
      unless @page.news?
        @url_name = url_for(:controller => :home, :action => :index).to_s
        if params[:page][:state] == "draft"  
          begin
            @page.send_email("reject",@url_name)        
          rescue Timeout::Error
            flash[:notice] = "Email Server is not responding.  Please send email manually." 
            @timeout = true
          end 
        elsif params[:review]
          begin
          	@page.send_email("pending",@url_name)	
          rescue Timeout::Error
            flash[:notice] = "Email Server is not responding.  Please send email manually."
            @timeout = true
          end	
    	  end
      end
      
      if @page.news?
        flash[:notice] = "News successfully edited"
        redirect_to news_path(@page)
      else  
        if @timeout == false
          flash[:notice] = @page.name + ' was updated'  
        end
        redirect_to :controller => @page.section.name, :action => :show, :id => @page
      end
    else
      render :action => "edit"
    end
    
  end
  
  def destroy
    @page = Page.find(params[:id])
    @page.destroy
    flash[:notice] = "Page successfully deleted."
    redirect_to :controller => @page.section.name
  end
  
end